home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / smtf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  4.3 KB  |  179 lines

  1. /* Copyright (C) 1995, 1996, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: smtf.c,v 1.2 2000/09/19 19:00:51 lpd Exp $ */
  20. /* MoveToFront filters */
  21. #include "stdio_.h"
  22. #include "strimpl.h"
  23. #include "smtf.h"
  24.  
  25. /* ------ MoveToFrontEncode/Decode ------ */
  26.  
  27. private_st_MTF_state();
  28.  
  29. /* Initialize */
  30. private int
  31. s_MTF_init(stream_state * st)
  32. {
  33.     stream_MTF_state *const ss = (stream_MTF_state *) st;
  34.     int i;
  35.  
  36.     for (i = 0; i < 256; i++)
  37.     ss->prev.b[i] = (byte) i;
  38.     return 0;
  39. }
  40.  
  41. /* Encode a buffer */
  42. private int
  43. s_MTFE_process(stream_state * st, stream_cursor_read * pr,
  44.            stream_cursor_write * pw, bool last)
  45. {
  46.     stream_MTF_state *const ss = (stream_MTF_state *) st;
  47.     register const byte *p = pr->ptr;
  48.     register byte *q = pw->ptr;
  49.     const byte *rlimit = pr->limit;
  50.     uint count = rlimit - p;
  51.     uint wcount = pw->limit - q;
  52.     int status =
  53.     (count < wcount ? 0 : (rlimit = p + wcount, 1));
  54.  
  55.     while (p < rlimit) {
  56.     byte b = *++p;
  57.     int i;
  58.     byte prev = b, repl;
  59.  
  60.     for (i = 0; (repl = ss->prev.b[i]) != b; i++)
  61.         ss->prev.b[i] = prev, prev = repl;
  62.     ss->prev.b[i] = prev;
  63.     *++q = (byte) i;
  64.     }
  65.     pr->ptr = p;
  66.     pw->ptr = q;
  67.     return status;
  68. }
  69.  
  70. /* Stream template */
  71. const stream_template s_MTFE_template = {
  72.     &st_MTF_state, s_MTF_init, s_MTFE_process, 1, 1
  73. };
  74.  
  75. /* Decode a buffer */
  76. private int
  77. s_MTFD_process(stream_state * st, stream_cursor_read * pr,
  78.            stream_cursor_write * pw, bool last)
  79. {
  80.     stream_MTF_state *const ss = (stream_MTF_state *) st;
  81.     register const byte *p = pr->ptr;
  82.     register byte *q = pw->ptr;
  83.     const byte *rlimit = pr->limit;
  84.     uint count = rlimit - p;
  85.     uint wcount = pw->limit - q;
  86.     int status = (count <= wcount ? 0 : (rlimit = p + wcount, 1));
  87.  
  88.     /* Cache the first few entries in local variables. */
  89.     byte
  90.     v0 = ss->prev.b[0], v1 = ss->prev.b[1],
  91.     v2 = ss->prev.b[2], v3 = ss->prev.b[3];
  92.  
  93.     while (p < rlimit) {
  94.     byte first;
  95.  
  96.     /* Zeros far outnumber all other bytes in the BWBS */
  97.     /* code; check for them first. */
  98.     if (*++p == 0) {
  99.         *++q = v0;
  100.         continue;
  101.     }
  102.     switch (*p) {
  103.         default:
  104.         {
  105.             uint b = *p;
  106.             byte *bp = &ss->prev.b[b];
  107.  
  108.             *++q = first = *bp;
  109. #if arch_sizeof_long == 4
  110.             ss->prev.b[3] = v3;
  111. #endif
  112.             /* Move trailing entries individually. */
  113.             for (;; bp--, b--) {
  114.             *bp = bp[-1];
  115.             if (!(b & (sizeof(long) - 1)))
  116.                      break;
  117.             }
  118.             /* Move in long-size chunks. */
  119.             for (; (b -= sizeof(long)) != 0;) {
  120.             bp -= sizeof(long);
  121.  
  122. #if arch_is_big_endian
  123.             *(ulong *) bp =
  124.                 (*(ulong *) bp >> 8) |
  125.                 ((ulong) bp[-1] << ((sizeof(long) - 1) * 8));
  126.  
  127. #else
  128.             *(ulong *) bp = (*(ulong *) bp << 8) | bp[-1];
  129. #endif
  130.             }
  131.         }
  132. #if arch_sizeof_long > 4    /* better be 8! */
  133.         goto m7;
  134.         case 7:
  135.         *++q = first = ss->prev.b[7];
  136. m7:        ss->prev.b[7] = ss->prev.b[6];
  137.         goto m6;
  138.         case 6:
  139.         *++q = first = ss->prev.b[6];
  140. m6:        ss->prev.b[6] = ss->prev.b[5];
  141.         goto m5;
  142.         case 5:
  143.         *++q = first = ss->prev.b[5];
  144. m5:        ss->prev.b[5] = ss->prev.b[4];
  145.         goto m4;
  146.         case 4:
  147.         *++q = first = ss->prev.b[4];
  148. m4:        ss->prev.b[4] = v3;
  149. #endif
  150.         goto m3;
  151.         case 3:
  152.         *++q = first = v3;
  153. m3:        v3 = v2, v2 = v1, v1 = v0, v0 = first;
  154.         break;
  155.         case 2:
  156.         *++q = first = v2;
  157.         v2 = v1, v1 = v0, v0 = first;
  158.         break;
  159.         case 1:
  160.         *++q = first = v1;
  161.         v1 = v0, v0 = first;
  162.         break;
  163.     }
  164.     }
  165.     ss->prev.b[0] = v0;
  166.     ss->prev.b[1] = v1;
  167.     ss->prev.b[2] = v2;
  168.     ss->prev.b[3] = v3;
  169.     pr->ptr = p;
  170.     pw->ptr = q;
  171.     return status;
  172. }
  173.  
  174. /* Stream template */
  175. const stream_template s_MTFD_template = {
  176.     &st_MTF_state, s_MTF_init, s_MTFD_process, 1, 1,
  177.     NULL, NULL, s_MTF_init
  178. };
  179.